<?php
 namespace app\api\model;
/**
 * @Author: user
 * @Date:   2019-04-03 19:49:41
 * @Last Modified by:   user
 * @Last Modified time: 2019-04-07 13:11:53
 */
use think\Model;
use think\Db;
/**
 * 
 */
class ApiLoginModel extends Model
{
	public function doLogin($data)
	{
		$result=Db::name('user')->where('user_login',$data['username'])->find();
		if(!empty($result) && $result['user_type']==1){
			if(cmf_compare_password($data['password'],$result['user_pass'])){
				$groups = Db::name('user_attach')
                    ->alias("a")->field('b.id,b.parent_id,b.company_id')
                    ->join('role b', 'a.role_id =b.id','LEFT')
                    ->where(["a.user_id" => $result["id"], "b.status" => 1])
                    ->find();
                    if ((empty($groups) || empty($result['user_status']))) {		          
		                return zy_json(false,'账户被停用！ ERRORCODE:E10001');
		            }
                    $active=Db::name('company')->where('id',$groups['company_id'])->find();
                    //检查是否是最高管理员
                    if($groups['parent_id']!=1){
                    	return zy_json(false,'NOT FOUND RELEVANT INFORMATION.');
                    }
                    if($active['active']>0){
                    	return zy_json(false,'无法进行注册登入，请联系管理员。 ERRORCODE:E10002');
                    }
                    //检查是否已经激活
	                if($result['user_status']==3){
	                    return zy_json(false,'申请账号未被通过！ ERRORCODE:E10003');
	                }
	                if($result['user_status']==2){
	                   	return zy_json(false,'申请账号正在审核中，请等待！ ERRORCODE:E10004');
	                }
	                return zy_json(true,'验证成功！',$result);
		    }else{
		    	//密码不正确
		    	return zy_json(false,'密码不正确！ ERRORCODE:E10005');
		    }
		}else{
			//账户不存在
			return zy_json(false,'账户不存在，请检查！ ERRORCODE:E10006');
		}
	}

	/**
	 * [getBaseInfo description]
	 * @return [type] [description]
	 */
	public function getBaseInfo($userId)
	{
		$user=Db::name('user')->where('id',$userId)->field('id,user_type,last_login_time,create_time,user_status,user_login,user_pass,user_nickname,user_email,avatar,last_login_ip,mobile')->find();
		$attach=Db::name('user_attach')->where('user_id',$userId)->find();
		$company=Db::name('company')->where('id',$attach['company_id'])->field('company_name,company_num,parent_id,status,company_type,create_time,versions_type,address,tel,url,remark')->find();
		$company['admin_id']=$user['id'];
		$company['company_id']=$attach['company_id'];
		unset($user['id']);
		$data=[
			'user'=>$user,
			'company'=>$company
		];
		return $data;
	}

	/**
	 * 更新激活状态
	 */
	public function updateActiveStatus($data)
	{
		$user=Db::name('user')->where('user_login',$data['username'])->find();
		if(empty($user) || !cmf_compare_password($data['password'],$user['user_pass']) ){
			return zy_json(false,'激活失败！');
		}
		$attach=Db::name('user_attach')->where('user_id',$user['id'])->find();
		Db::name('company')->where('id',$attach['company_id'])->update(['active'=>1,'active_date'=>time()]);
		return zy_json(true,'激活成功！');
	}

}